package com.baozun.houji.me.activity.account

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import com.baozhun.mall.common.base.BaseActivity
import com.baozun.houji.me.R
import com.baozun.houji.me.databinding.ActivityForgetPwdBinding
import com.baozun.houji.me.viewmodel.ForgetPwdViewModel
import com.jaeger.library.StatusBarUtil


/**
 * @Title：忘记密码页面
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/12/10
 * @Version：1.0.0
 */
class ForgetPwdActivity : BaseActivity<ForgetPwdViewModel, ActivityForgetPwdBinding>() {
    var timer: CountDownTimer? = null

    companion object {
        fun start(context: Context) {
            var intent = Intent(context, ForgetPwdActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setColor(this, getColor(android.R.color.transparent))
        StatusBarUtil.setTransparentForImageView(this, mDataBinding.titleBarBack)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.tv_get_verify_code -> mViewModel.getVerifyCode {
                startCountDownTimer()
            }

            R.id.tv_go_ahead_reset_pwd -> { //前去重置密码
                mViewModel.checkVerifyCode {
                    ResetPwdActivity.start(
                        this,
                        mViewModel.email.value.toString(),
                        mViewModel.code.value.toString(),
                        mViewModel.ticket
                    )
                }
            }

        }
    }

    /**
     * 开启倒计时
     */
    private fun startCountDownTimer() {
        timer?.cancel()
        timer = object : CountDownTimer(60 * 1000L, 1000L) {
            override fun onTick(millisUntilFinished: Long) {
                mViewModel.remainingTime.postValue(millisUntilFinished.toInt() / 1000)
            }

            override fun onFinish() {
                mViewModel.remainingTime.postValue(0)
            }

        }
        timer?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        timer?.cancel()
    }

    override fun createObserver() {

    }

}